package com.kmxd.ams.app.archive;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.archive.IArchiveAuthRuleSvc;
import com.kmxd.ams.client.archive.dto.ArchiveAuthRuleAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveAuthRuleEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveAuthRuleQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveAuthRuleListVO;
import com.kmxd.ams.client.archive.vo.ArchiveAuthRuleVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveAuthRule;
import com.kmxd.ams.infra.archive.mapper.ArchiveAuthRuleMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 特殊利用授权 服务实现类
 *
 * @author nickbi
 * @since 2022-07-27
 */
@Service
@AllArgsConstructor
public class ArchiveAuthRuleSvcImpl extends ServiceImpl<ArchiveAuthRuleMapper, ArchiveAuthRule>
    implements IArchiveAuthRuleSvc {

  private ArchiveAuthRuleMapper archiveAuthRuleMapper;

  @Override
  public Page<ArchiveAuthRuleListVO> selectPage(Page reqPage, ArchiveAuthRuleQueryDTO req) {
    LambdaQueryWrapper<ArchiveAuthRule> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveAuthRule>(), req).lambda();
    Page<ArchiveAuthRule> page = archiveAuthRuleMapper.selectPage(reqPage, queryWrapper);
    Page<ArchiveAuthRuleListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ArchiveAuthRuleListVO.class));
    return voPage;
  }

  @Override
  public List<ArchiveAuthRuleListVO> selectList(ArchiveAuthRuleQueryDTO req) {
    LambdaQueryWrapper<ArchiveAuthRule> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveAuthRule>(), req).lambda();
    List<ArchiveAuthRule> list = archiveAuthRuleMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveAuthRuleListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ArchiveAuthRuleAddDTO req) {
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    ArchiveAuthRule entity = BeanUtil.copyProperties(req, ArchiveAuthRule.class);
    archiveAuthRuleMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ArchiveAuthRuleEditDTO req) {
    ArchiveAuthRule entity = BeanUtil.copyProperties(req, ArchiveAuthRule.class);
    archiveAuthRuleMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public ArchiveAuthRuleVO view(String id) {
    ArchiveAuthRule entity = archiveAuthRuleMapper.selectById(id);
    return BeanUtil.copyProperties(entity, ArchiveAuthRuleVO.class);
  }
}
